﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MeTalker.Model;
using MeTalker.BLL;
public partial class Default_SoftForm : WebForm
{
    string id = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        id = Request.QueryString["param"];
        if (!Page.IsPostBack)
        {
            if (!string.IsNullOrEmpty(id))
            {
                BindData(id);
            }
        }
    }

    void BindData(string id)
    {
        SoftwaresMod soft = SoftwaresBLL.GetSoftwareByAutoID(id);
        if (soft != null)
        {
            txtDownloadURL.Text = soft.DownloadURL;
            txtSoftID.Text = soft.SoftID;
            txtSoftName.Text = soft.SoftName;
            txtTrialDays.Text = soft.TrialDays.ToString();
            txtUpdateInfo.Text = soft.UpdateInfo;
            txtVersion.Text = soft.Version;
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        SoftwaresMod soft = new SoftwaresMod();
        soft.DownloadURL = txtDownloadURL.Text;
        soft.SoftID = txtSoftID.Text;
        soft.SoftName = txtSoftName.Text;
        int day = 0;
        if (!int.TryParse(txtTrialDays.Text, out day))
        {
            Alert("请输入正确的天数!必须为整数!");
            return;
        }

        if (string.IsNullOrEmpty( soft.SoftID))
        {
            Alert("软件ID不能为空!");
            return;
        }
        if (string.IsNullOrEmpty(soft.SoftName))
        {
            Alert("软件名称不能为空!");
            return;
        }
        soft.TrialDays = day;
        soft.UpdateInfo = txtUpdateInfo.Text;
        soft.Version = txtVersion.Text;
        if (string.IsNullOrEmpty(id))
        {
            if (SoftwaresBLL.GetSoftware(soft.SoftID) != null) {
                Alert(soft.SoftID + "已经存在!");
                return;
            }
            if (SoftwaresBLL.Insert(soft) > 0)
            {
                Alert("添加软件成功!", "closePanel");
            }
            else
            {
                Alert("添加软件失败!");
            }
        }
        else
        {
            soft.AutoID = int.Parse(id);
            if (SoftwaresBLL.Update(soft) > 0)
            {
                Alert("添加软件成功!", "closePanel", "RefreshData");
            }
            else
            {
                Alert("添加软件失败!");
            }

        }
    }
}